<?php
namespace Admin\Controller;
use Think\Controller;
class SysController extends Controller {
	
	//分类首页
    public function index(){
    	if($_POST){
    		//获取公共配置文件汇总的数据
	    	$arr = include "./Application/Conf/config.php";
	    	
	    	//判断是否修改logo
	    	if($_POST['LOGO'] == $_POST['old_logo']){
	    		
	    	}else{
	    		unlink("./Uploads/$_POST[old_logo]");
	    		unlink("./Uploads/sm_$_POST[old_logo]");
	    		unlink("./Uploads/md_$_POST[old_logo]");
	    		unlink("./Uploads/lg_$_POST[old_logo]");
	    		unlink("./Uploads/water_$_POST[old_logo]");
	    	}
	    	unset($_POST['old_logo']);
	    	//合并数组
	    	$brr = array_merge($arr, $_POST);
	    	//遍历字符串写入源文件
	    	foreach($brr as $key => $value){
	    		$str .= "'$key' => '$value',\n";
	    	}
	    	$newStr = "<?php return array( \n$str \n)?>";
	    	print_r($newStr);exit;	
	    	file_put_contents("./Application/Conf/config.php", $newStr);
	    	$this->success("配置成功");
    	}else{
    		$this->display();
    	}
    }
    
    //轮播首页
    public function lunbo_list(){
    	$model = M('slider');
    	
    	$this->total = $model->count();
    	
    	$this->data = $model->order('sort desc')->select();
    	$this->display();
    }
    
    //轮播添加
    public function lunbo_add(){
    	$this->display();
    }
    
    //轮播插入
    public function lunbo_insert(){
    	$model = M('slider');
    	
    	if($model->add($_POST)){
    		$this->success('添加成功',U("lunbo_list"));
		}else{
			$this->error('添加失败');
		}
    }
    
    //轮播修改
    public function lunbo_edit($id){
    	$model = M('slider');
    	$this->data = $model->find($id);
    	$this->display();
    }
    //更新保存
    public function lunbo_update(){
    	$model = M('slider');
    	
    	if($model->save($_POST)){
    		if($_POST['img'] == $_POST['old_img']){
    			
    		}else{
    			unlink("./Uploads/$_POST[old_img]");
	    		unlink("./Uploads/sm_$_POST[old_img]");
	    		unlink("./Uploads/md_$_POST[old_img]");
	    		unlink("./Uploads/lg_$_POST[old_img]");
	    		unlink("./Uploads/water_$_POST[old_img]");
    		}
    		
    		$this->success('修改成功',U("lunbo_list"));
		}else{
			$this->error('修改失败');
		}
    }
    
    //处理ajax请求
    public function ajax_sorts(){
    	if(M('slider')->save($_POST)){
    		echo 1;
    	}else{
    		echo 0;
    	}
    }
    
    //处理ajax请求
    public function ajax_del(){
    	$id = $_POST['id'];
    	$model = M('slider');
    	$data = $model->find($id);
    	if($model->delete($id)){
    		unlink("./Uploads/$data[img]");
    		unlink("./Uploads/sm_$data[img]");
    		unlink("./Uploads/md_$data[img]");
    		unlink("./Uploads/lg_$data[img]");
    		unlink("./Uploads/water_$data[img]");
    		echo 1;
    		
    		
    	}else{
    		echo 0;
    	}
    }
    
    //无刷新上传图片
    public function ajax_up(){
//  	print_r($_FILES);
    	//echo 1;
     	if(!empty($_FILES)){
     		//图片上传设置
     		$upload = new \Think\Upload();//实例化上传类
     		$upload->autoSub = false;
     		$images = $upload->upload();
     		//判断是否有图
     		if($images){//print_r($images);
//   			$info = $images['Filedata']['savepath'] . $images['Filedata']['savename'];
     			$info = $images['ups']['savepath'] . $images['ups']['savename'];
     			//返回文件地址和名字给js作回调用
     			$path = $info;
     			//对图片进行裁剪和水印处理
     			$image = new \Think\Image();
     			//打开文件-并进行缩放
     			$image->open('./Uploads/' . $path)->thumb(800,800)->save('./Uploads/lg_' . $path);
     			$image->open('./Uploads/' . $path)->thumb(500,500)->save('./Uploads/md_' . $path);
     			$image->open('./Uploads/' . $path)->thumb(100,100)->save('./Uploads/sm_' . $path);
     			
     			//打开文件-并进行水印
       			$image->open('./Uploads/' . $path)->water('./Uploads/water/1.jpg', 5)->save('./Uploads/water_' . $path);
     			
     			echo $info;
     		}else{
     			$this->error($upload->getError());//获取失败信息
     		}
     	}
     	else{
     		echo 1;
     	}
    }
}